local zhongquan = fk.CreateSkill{
    name = "ym2__zhongquan",
    tags = {Skill.Permanent},
}
Fk:loadTranslationTable{
    ["ym2__zhongquan"] = "踵权",
    [":ym2__zhongquan"] = "持恒技，出牌阶段各限一次，你可以发动“结姻”或“离间”，体力值因此变化的角色交换主副将。",
    ["ym2__zhongquan-jieyin"] = "结姻",
    ["#ym2__zhongquan-jieyin"] = "踵权：弃一张手牌或将一张装备置入一名角色装备区，你与其体力值较少的角色回复1点体力，较多的角色摸一张牌",
    ["#ym2__zhongquan-jieyin-card"] = "结姻：弃置%arg，或将之置入%dest的装备区",
    ["ex__jieyin_discard"] = "弃置手牌",
    ["ex__jieyin_put"] = "置入装备",
    ["ym2__zhongquan-lijian"] = "离间",
    ["#ym2__zhongquan-lijian"] = "踵权：弃置一张手牌并选择两名其他男性角色，后选择的角色视为对先选择的角色使用了一张不能被【无懈可击】的【决斗】",
}
zhongquan:addEffect("active",{
    anim_type = "control",
    prompt = function (self, player, selected_cards, selected_targets)
        if self.interaction.data == "ym2__zhongquan-lijian" then
            return "#ym2__zhongquan-lijian"
        else
            return "#ym2__zhongquan-jieyin"
        end
    end,
    interaction = function (self, player)
        local all_choices = {"ym2__zhongquan-jieyin","ym2__zhongquan-lijian"}
        local choices = {}
        for _, value in ipairs(all_choices) do
            if not table.contains(player:getTableMark("ym2__zhongquan-phase"),value) then
                table.insertIfNeed(choices,value)
            end
        end
        return UI.ComboBox{choices = choices}
    end,
    can_use = function(self, player)
        return #player:getTableMark("ym2__zhongquan-phase") < 2
    end,
    card_num = 1,
    card_filter = function (self, player, to_select, selected)
        if not self.interaction.data then return false end
        if self.interaction.data == "ym2__zhongquan-jieyin" then
            if #selected > 0 then return false end
            return Fk:getCardById(to_select).type == Card.TypeEquip or (not player:prohibitDiscard(to_select) and table.contains(player:getCardIds("h"), to_select))
        else
            return #selected == 0 and not player:prohibitDiscard(to_select)
        end
    end,
    target_filter = function(self, player, to_select, selected, cards)
        if not self.interaction.data then return false end
        if self.interaction.data == "ym2__zhongquan-jieyin" then
            if #cards ~= 1 or #selected > 0 or to_select == player then return false end
            return to_select:isMale() and ((not player:prohibitDiscard(cards[1]) and table.contains(player:getCardIds("h"), cards[1])) or
            ( Fk:getCardById(cards[1]).type == Card.TypeEquip and to_select:hasEmptyEquipSlot(Fk:getCardById(cards[1]).sub_type)))
        else
            if #selected < 2 and to_select ~= player and to_select:isMale() then
                if #selected == 0 then
                    return true
                else
                    return to_select:canUseTo(Fk:cloneCard("duel"), selected[1])
                end
            end
        end
    end,
    target_tip = function(self, _, to_select, selected, _, _, selectable, _)
        if self.interaction.data == "ym2__zhongquan-jieyin" or not selectable then return end
        if #selected == 0 or (#selected > 0 and selected[1] == to_select) then
            return "lijian_tip_1"
        else
            return "lijian_tip_2"
        end
    end,
    feasible = function (self, player, selected, selected_cards, card)
        if not self.interaction.data then return false end
        if self.interaction.data == "ym2__zhongquan-jieyin" then
            return #selected == 1 and #selected_cards == 1
        else
            return #selected == 2 and #selected_cards == 1
        end
    end,
    on_use = function (self, room, effect)
        local player = effect.from
        local changePlayer = {}
        room:addTableMark(player,"ym2__zhongquan-phase",self.interaction.data)
        if self.interaction.data == "ym2__zhongquan-jieyin" then
            local choices = {}
            local to = effect.tos[1]
            local card = Fk:getCardById(effect.cards[1])
            if not player:prohibitDiscard(card) and table.contains(player:getCardIds("h"),effect.cards[1]) then
                table.insertIfNeed(choices,"ex__jieyin_discard")
            end
            if card.type == Card.TypeEquip and to:hasEmptyEquipSlot(card.sub_type) then
                table.insertIfNeed(choices,"ex__jieyin_put")
            end
            if #choices > 0 then
                local choice = room:askToChoice(player,{
                    choices = choices,
                    skill_name = "ym2__zhongquan-jieyin",
                    prompt = "#ym2__zhongquan-jieyin-card::"..to.id..":"..card:toLogString()
                })
                if choice == "ex__jieyin_put" then
                    room:moveCardIntoEquip(to, effect.cards, zhongquan.name, false, player)
                else
                    room:throwCard(effect.cards, zhongquan.name, player, player)
                end
            else
                room:throwCard(effect.cards, zhongquan.name, player, player)
            end
            if to.hp < player.hp then
                if to:isWounded() and not to.dead then
                    local tempHp = to.hp
                    room:recover{
                        who = to,
                        num = 1,
                        recoverBy = player,
                        skillName = zhongquan.name,
                    }
                    if tempHp ~= to.hp then
                        table.insert(changePlayer,to)
                    end
                end
                if not player.dead then
                    player:drawCards(1, zhongquan.name)
                end
            elseif to.hp > player.hp then
                if player:isWounded() and not player.dead then
                    local tempHp = player.hp
                    room:recover({
                        who = player,
                        num = 1,
                        recoverBy = player,
                        skillName = zhongquan.name
                    })
                    if tempHp ~= player.hp then
                        table.insert(changePlayer,player)
                    end
                end
                if not to.dead then
                    to:drawCards(1, zhongquan.name)
                end
            end
        else
            room:throwCard(effect.cards, zhongquan.name, player, player)
            local duel = Fk:cloneCard("duel")
            duel.skillName = zhongquan.name
            local use = { ---@type UseCardDataSpec
                from = effect.tos[2],
                tos = { effect.tos[1] },
                card = duel,
                prohibitedCardNames = { "nullification" },
            }
            room:useCard(use)
            if use.damageDealt then
                for _, p in ipairs(room:getAlivePlayers()) do
                    if use.damageDealt[p] and not p.dead then
                        table.insert(changePlayer,p)
                    end
                end
            end
        end
        for _, cp in ipairs(changePlayer) do
            if cp.deputyGeneral ~= "" then
                local general = cp.general ~= "" and cp.general or "blank_shibing"
                local deputyGeneral = cp.deputyGeneral ~= "" and cp.deputyGeneral or "blank_shibing"
                --room:changeHero(cp,general,false,true,true,true,false)
                --room:changeHero(cp,deputyGeneral,false,false,true,true,false)
                room:prepareGeneral(cp,deputyGeneral,general,true)
            end
        end
    end,
})
return zhongquan